package com.konke.smartpad_huahao.UI.presenter;

import android.text.TextUtils;

import com.konke.sdslibrary.ISdsDomain;
import com.konke.sdslibrary.event.DeviceBindEvent;
import com.konke.sdslibrary.net.response.BaseResp;
import com.konke.sdslibrary.net.response.BindQrCodeResp;
import com.konke.sdslibrary.net.response.RegisterDeviceResp;
import com.konke.smartpad_huahao.MyApplication;
import com.konke.smartpad_huahao.UI.MainContract;
import com.zsj.comminlibrary.base.BasePresenter;
import com.zsj.comminlibrary.util.GetDeviceInfoResp;
import com.zsj.comminlibrary.util.NetworkUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class MainPresenter extends BasePresenter<MainContract.IMainView> implements MainContract.IMainPresenter {
    private boolean finish = false;
    private boolean hasRegsiter = false;

    @Override
    public void onCreate() {
        super.onCreate();
        EventBus.getDefault().register(this);
    }

    @Override
    public void getDeviceStatus() {
        boolean b = NetworkUtil.hasNetwork(mContext);
        if (!b) {
            return;
        }
        ISdsDomain iSdsDomain = MyApplication.sISdsDomain;
        String id = iSdsDomain.getId();
        if (id == null || TextUtils.isEmpty(id)) {
            registerDevice();
        } else {
            getDeviceInfo();
        }
    }

    private void registerDevice() {
        Observable<BaseResp<BindQrCodeResp>> observable = Observable.create(emitter -> {
            ISdsDomain iSdsDomain = MyApplication.sISdsDomain;
            String id = iSdsDomain.getId();
            BaseResp<RegisterDeviceResp> resp = iSdsDomain.registerDevice();
            BaseResp<BindQrCodeResp> codeRespBaseResp = iSdsDomain.bindQrCode();
            hasRegsiter = true;
            getDeviceInfo();
            emitter.onNext(codeRespBaseResp);
        });
        Disposable subscribe = observable.observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(bindQrCodeRespBaseResp -> {
                    if (bindQrCodeRespBaseResp == null || !bindQrCodeRespBaseResp.isSuccess()) {
                        mView.showToast("获取二维码失败");
                    } else {
                    }
                });
        addDisposable(subscribe);
    }

    private void getDeviceInfo() {
        Observable<Boolean> observable = Observable.create(emitter -> {
            ISdsDomain iSdsDomain = MyApplication.sISdsDomain;
            BaseResp<GetDeviceInfoResp> devInfo = iSdsDomain.getDevInfo();
            if (devInfo == null || !devInfo.isSuccess()) {
                emitter.onNext(false);
            } else {
                GetDeviceInfoResp data = devInfo.getData();
                if (data.getBindUserId() == null || TextUtils.isEmpty(data.getBindUserId())) {
                    emitter.onNext(false);
                } else {
                    iSdsDomain.updatePushToken();
                    emitter.onNext(true);
                }
            }
        });
        Disposable subscribe = observable.observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(aBoolean -> {
                    if (aBoolean) {
                        if (!finish) {
                            mView.exit();
                        }
                    } else {
                        if (!hasRegsiter) {
                            registerDevice();
                        }
                    }
                });
        addDisposable(subscribe);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onDeviceBindEvent(DeviceBindEvent event) {
        finish = true;
        mView.exit();
    }
}
